/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utilitarios;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Welisson
 */
public class Data {
    
//    public static void main(String[] args){
//        Data.dataValida("22061988");
//    }
    
    public static Date stringToDate(String data){
        DateFormat formater = new SimpleDateFormat("dd/MM/yyyy"); 
        formater.setLenient(false); 
        Date date = null; 
        try {
            date = formater.parse(data);
        } catch (ParseException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Erro ao Converter data!");
        }
//        System.out.println("Data: "+date);
        return date;
    }
    
    public static boolean dataValida(String data){
        int size = data.toCharArray().length;
        int dia;
        int mes;
        int ano;
        if(size == 10){
            dia = Integer.parseInt(data.substring(0, 2));
            mes = Integer.parseInt(data.substring(3, 5));
            ano = Integer.parseInt(data.substring(6, 10));
        }else if(size == 8){
            dia = Integer.parseInt(data.substring(0, 2));
            mes = Integer.parseInt(data.substring(2, 4));
            ano = Integer.parseInt(data.substring(4, 8));
        }else{
            return false;
        }
        
        if(dia <= 31){
            if(mes <= 12){
                if(ano >= 1500){
                    return true;
                }
            }
        }
        return false;
    }
    
    public static String dateToString(Date data){
        DateFormat formater = new SimpleDateFormat("dd/MM/yyyy"); 
        formater.setLenient (false); 
        
            
//        System.out.println("Data: "+date);
        return formater.format(data);
    }
    
    public static Date dataDeHoje(){
        Date data = new Date();  
        
        return data;
    }
}
